package com.cvicse.jrcompetition01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cvicse.jrcompetition01.common.lang.Result;
import com.cvicse.jrcompetition01.entity.Department;
import com.cvicse.jrcompetition01.mapper.DepartmentMapper;
import com.cvicse.jrcompetition01.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cvicse.jrcompetition01.utils.AutoGenerateLastCodeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 作者:kissshotwang@163.com
 * @since 2022-07-22
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
    @Resource
    DepartmentMapper departmentMapper;

    @Resource
    AutoGenerateLastCodeUtil autoGenerateLastCodeUtil;

    @Override
    public Integer add(Department department) {

        List<Department> departmentList = departmentMapper.selectList(new QueryWrapper<Department>());
        //排序
        Collections.sort(departmentList);

        //考虑函数复用
        int size = 1;
        //考虑函数复用
        if (departmentList.size() != 0) {
            size = departmentList.get(departmentList.size()-1).getId();
        }
        String uuid = "";
        if (null != autoGenerateLastCodeUtil.getMap().get("-") && !autoGenerateLastCodeUtil.getMap().get("-").equals("01")) {
            autoGenerateLastCodeUtil.getMap().put("-", "01");
        }
        for (int i = 0; i < size+1; i++) {
            uuid = autoGenerateLastCodeUtil.getNum();
        }

        department.setMechanismDepartmentNumber(department.getMechanismDepartmentNumber()+uuid);
        department.setCreated(LocalDate.now());
        int i = departmentMapper.insert(department);
        return i;

    }
}
